# This file is part of The Vu Toolkit.
#
# The Vu Toolkit is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3.0 of the License, or (at your option) any later version.
#
# The Vu Toolkit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with The Vu Toolkit; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA.
#
# Copyright © Zak Fenton 2008
#

# A mixin module giving SVG elements X/Y/Width/Height functionality.
# Retrieving the x/y/width/height attributes always results in a number, in
# terms of the element's transformation matrix and display properties. These
# values can, however, be set as either an Abstract, or a plain number.
#
# The method bounds_modified is called when any of these values are changed.
#
# FIXME: actually allow abstract values ;)
module Vu::SVG::XYWidthHeight
	# Width of a centimeter in pixels/whatever
	def cm_width
		35
	end
	
	# Height of a centimeter in pixels/whatever
	def cm_height
		35
	end
	
	# Converts an abstract along the X axis to pixels (returns a if number)
	def to_pixels_x (a)
		if a.is_a?(Vu::SVG::Abstract)
			case a.units
				when :cm
					cm_width*a.value
				else
					a.value
			end
		else
			a
		end
	end
	
	def to_pixels_y (a)
		to_pixels_x (a)
	end

	def x
		to_pixels_x @x
	end
	
	def y
		to_pixels_y @y
	end
	
	def width
		to_pixels_x @width
	end
	
	def height
		to_pixels_y @height
	end
	
	def x= (v)
		@x = v
		bounds_modified
	end
	
	def y= (v)
		@y = v
		bounds_modified
	end
	
	def width= (v)
		@width = v
		bounds_modified
	end
	
	def height= (v)
		@height = v
		bounds_modified
	end
end
